//ExceptionTest.java
package com.ylaihui.exception;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;
import org.junit.Test;

public class ExceptionTest {
	
	@Test
	public void test6(){
		// checked exception 编译时异常
		File file = new File("1.txt");
		// Unhandled exception type FileNotFoundException
		FileInputStream fis = new FileInputStream(file);
		
		// Unhandled exception type IOException
		int data = fis.read();
		while(data != -1){
			System.out.print((char)data);
			// Unhandled exception type IOException
			data = fis.read();
		}
		// Unhandled exception type IOException
		fis.close();
	}
	
	@Test
	public void test5(){
		// ArithmeticException  / by zero
		System.out.println(12/0);
	}
	
	@Test
	public void test4(){
		Scanner scan = new Scanner(System.in);
		int nint = scan.nextInt(); // 输入abc， 则 InputMismatchException
	}
	
	@Test
	public void test3(){
		// NumberFormatException
		String str = "add";
		Integer i = Integer.parseInt(str);
	}
	
	@Test
	public void test2(){
		// ClassCastException
		Object string = new String();
		Date d = (Date)string;
	}
	
	@Test
	public void test1(){
		// StringIndexOutOfBoundsException
		String str = "aaa";
		System.out.println(str.charAt(3));
	}
	
	public static void main(String[] args) {
		// java.lang.NullPointerException
//		String str = null;
//		System.out.println(str.charAt(1));
		
		// java.lang.ArrayIndexOutOfBoundsException
//		int[] arrary = new int[2];
//		System.out.println(arrary[2]);
	}
}
